package com.yang.sso.oauth.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.yang.sso.oauth.enums.ResultCode;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;


/**
 *  统一响应结构体
 * @author: Islands
 * @create: 2024-03-29 17:27
 */
@Data
@NoArgsConstructor
@JsonPropertyOrder(value = {"flag", "code","message","data"})
public class Result<T> implements Serializable {

    /**
     * 请求是否成功
     */
    private Boolean flag;
    /**
     * 状态码
     * @mock 2000
     * @apiNote  20* <!--成功-->;40* <!--失败-->;50* <!--错误-->
     */
    private int code;
    /**
     * 返回消息
     */
    private String message;
    /**
     * 返回数据
     */
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private T data;


    public Result(Boolean flag, ResultCode resultCode) {
        this.flag = flag;
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
    }
    public Result(Boolean flag,String message) {
        this.flag = flag;
        this.code = ResultCode.FAIL.getCode();
        this.message =message;
    }
    public Result(Boolean flag, ResultCode resultCode,T data) {
        this.flag = flag;
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
        this.data = data;
    }
    public Result(Boolean flag, ResultCode resultCode,String message) {
        this.flag = flag;
        this.code = resultCode.getCode();
        this.message =message;
    }

    public static <T> Result<T> success(T data) {
        return new Result<T>(true,ResultCode.SUCCESS,data);
    }
    public static <T> Result<T> success(ResultCode code,T data) {
        return new Result<T>(true,code,data);
    }

    public static <T> Result<T> failed(String message) {
        return new Result<T>(false,message);
    }
    public static <T> Result<T> failed(ResultCode code) {
        return new Result<T>(false,code);
    }

    public static <T> Result<T> failed(ResultCode code,String message) {
        return new Result<T>(false,code,message);
    }
}
